package vip.idiy.anwei_android_iot.utils.dbHandle;

import android.content.ContentValues;
import android.database.Cursor;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

import vip.idiy.anwei_android_iot.domin.DeviceType;
import vip.idiy.anwei_android_iot.utils.DatabaseUtil;

/**
 * ClassName TypeDataHandle
 * describe:
 * Created: 勇
 * Email:118085890@qq.com
 * Version: 1.0
 * Create in 17:06 2023/4/21
 */
public class TypeDataHandle {
    DatabaseUtil databaseUtil;

    private final String DATA_TABLE = "device_type";
    // 每页返回记录数
    private final int LIMIT = 10;

    private final String COLUMNS_TYPE_ID = "type_id";
    private final String COLUMNS_TYPE_NAME = "type_name";
    private final String COLUMNS_TYPE_IMG = "type_img";
    // 查询条件
    private final String SELECTION_TYPE_NAME = "type_name = ? ";
    private final String SELECTION_TYPE_ID = "type_id = ? ";

    public TypeDataHandle(DatabaseUtil databaseUtil) {
        this.databaseUtil = databaseUtil;
    }

    /**
     * 添加
     *
     * @param deviceType
     * @return
     */
    public boolean insert(DeviceType deviceType) {
        if (StringUtils.isEmpty(deviceType.getTypeName()) || StringUtils.isEmpty(deviceType.getTypeImg()))
            return false;
        ContentValues values = new ContentValues();
        values.put(COLUMNS_TYPE_NAME, deviceType.getTypeName());
        values.put(COLUMNS_TYPE_IMG, deviceType.getTypeImg());
        return databaseUtil.insert(DATA_TABLE, values) > 0;
    }

    /**
     * 根据typeId更新数据
     *
     * @param deviceType
     * @return
     */
    public boolean update(DeviceType deviceType) {
        ContentValues values = new ContentValues();
        values.put(COLUMNS_TYPE_NAME, deviceType.getTypeName());
        values.put(COLUMNS_TYPE_IMG, deviceType.getTypeImg());
        return databaseUtil.update(DATA_TABLE, values, SELECTION_TYPE_ID, new String[]{String.valueOf(deviceType.getTypeId())}) > 0;
    }

    /**
     * 根据typeId删除数据
     *
     * @param deviceType
     * @return
     */
    public boolean delete(DeviceType deviceType) {
        return databaseUtil.delete(DATA_TABLE, SELECTION_TYPE_ID, new String[]{String.valueOf(deviceType.getTypeId())}) > 0;
    }

    /**
     * 根据位置名称获取记录
     *
     * @param typeName
     * @return
     */
    public DeviceType selectByTypeName(String typeName) {
        Cursor cursor = databaseUtil.query(DATA_TABLE,
                new String[]{COLUMNS_TYPE_ID, COLUMNS_TYPE_NAME, COLUMNS_TYPE_IMG},
                SELECTION_TYPE_NAME,
                new String[]{typeName}, null, null, null);
        DeviceType deviceType = new DeviceType();
        if (cursor != null && cursor.moveToFirst()) {
            int idIndex = cursor.getColumnIndex(COLUMNS_TYPE_ID);
            if (idIndex >= 0) {
                deviceType.setTypeId(cursor.getInt(idIndex));
            }
            int nameIndex = cursor.getColumnIndex(COLUMNS_TYPE_NAME);
            if (nameIndex >= 0) {
                deviceType.setTypeName(cursor.getString(nameIndex));
            }
            int imgIndex = cursor.getColumnIndex(COLUMNS_TYPE_IMG);
            if (imgIndex >= 0) {
                deviceType.setTypeImg(cursor.getString(imgIndex));
            }
        }
        return deviceType;
    }

    /**
     * 按位置名称查询记录是否存在
     *
     * @param typeName
     * @return
     */
    public boolean isExist(String typeName) {
        Cursor cursor = null;
        try {
            String[] columns = new String[]{COLUMNS_TYPE_NAME};
            String[] selectionArgs = new String[]{typeName};
            cursor = databaseUtil.query(DATA_TABLE, columns, SELECTION_TYPE_NAME, selectionArgs, null, null, null);
            return cursor.getCount() > 0;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        return false;
    }

    /**
     * 获取列表
     *
     * @param columnNames
     * @param columnValues
     * @return
     */
    public List<DeviceType> getList(String[] columnNames, String[] columnValues) {
        List<DeviceType> dataList = new ArrayList<>();
        Cursor cursor = null;
        try {
            // 拼接查询参数
            StringBuilder selectionBuilder = new StringBuilder();
            if (columnNames == null || columnValues == null || columnNames.length == 0 || columnValues.length == 0) {
                cursor = databaseUtil.query(DATA_TABLE, null, null, null, null, null, null);

            } else {
                for (int i = 0; i < columnNames.length; i++) {
                    selectionBuilder.append(columnNames[i]).append("=?");
                    if (i != columnNames.length - 1) {
                        selectionBuilder.append(" AND ");
                    }
                }
                String selection = selectionBuilder.toString();
                cursor = databaseUtil.query(DATA_TABLE, null, selection, columnValues, null, null, null);
            }
            while (cursor.moveToNext()) {
                DeviceType data = new DeviceType();
                int idIndex = cursor.getColumnIndex(COLUMNS_TYPE_ID);
                if(idIndex>=0){
                    data.setTypeId(cursor.getInt(idIndex));
                }
                int nameIndex = cursor.getColumnIndex(COLUMNS_TYPE_NAME);
                if (nameIndex >= 0) {
                    data.setTypeName(cursor.getString(nameIndex));
                }
                int imgIndex = cursor.getColumnIndex(COLUMNS_TYPE_IMG);
                if (imgIndex >= 0) {
                    data.setTypeImg(cursor.getString(imgIndex));
                }
                dataList.add(data);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        return dataList;
    }
    /**
     * 获取列表
     *
     * @param columnNames
     * @param columnValues
     * @return
     */
    public List<DeviceType> getList(String[] columnNames, String[] columnValues, int pageNum) {
        List<DeviceType> dataList = new ArrayList<>();
        Cursor cursor = null;
        try {
            // 计算起始记录的偏移量
            int offset = (pageNum - 1) * LIMIT;
            // 拼接查询参数
            StringBuilder selectionBuilder = new StringBuilder();
            if (columnNames == null || columnValues == null || columnNames.length == 0 || columnValues.length == 0) {
                cursor = databaseUtil.query(DATA_TABLE, null, null, null, null, null, null, offset + "," + LIMIT);

            } else {
                for (int i = 0; i < columnNames.length; i++) {
                    selectionBuilder.append(columnNames[i]).append("=?");
                    if (i != columnNames.length - 1) {
                        selectionBuilder.append(" AND ");
                    }
                }
                String selection = selectionBuilder.toString();
                cursor = databaseUtil.query(DATA_TABLE, null, selection, columnValues, null, null, null, offset + "," + LIMIT);
            }
            while (cursor.moveToNext()) {
                DeviceType data = new DeviceType();
                int nameIndex = cursor.getColumnIndex(COLUMNS_TYPE_NAME);
                if (nameIndex >= 0) {
                    data.setTypeName(cursor.getString(nameIndex));
                }
                int imgIndex = cursor.getColumnIndex(COLUMNS_TYPE_IMG);
                if (imgIndex >= 0) {
                    data.setTypeImg(cursor.getString(imgIndex));
                }
                dataList.add(data);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        return dataList;
    }

    /**
     * 获取总总记录数
     *
     * @return
     */
    public int getTotalRecordCount(String[] columnValues) {
        int count = 0;
        Cursor cursor = null;
        try {
            if (columnValues == null) {
                cursor = databaseUtil.rawQuery("SELECT COUNT(*) FROM " + DATA_TABLE, null);
            } else {
                cursor = databaseUtil.rawQuery("SELECT COUNT(*) FROM " + DATA_TABLE + " WHERE type_name = ?", columnValues);
            }

            if (cursor.moveToFirst()) {
                count = cursor.getInt(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        return count;
    }
}
